from django.shortcuts import render

# Create your views here.
from rest_framework.viewsets import ModelViewSet
from utils.token_utils import login_token
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSet
from rest_framework.response import Response
from django.conf import settings
from .models import UserModel, AddressModel
from .serializer import UserSer, AddressSer

from django.contrib.auth.hashers import make_password, check_password
import datetime

# 用户登录注册
class UserViewsRegister(APIView):
    def post(self, request):
        try:
            nickname = request.data.get("nickname")
            password = request.data.get("password")
            mobile = request.data.get("mobile")
            print("数据",nickname,password,mobile)

            if not all([nickname,password,mobile]):
                return Response({"code": 400,"message": "注册信息不完整"})

            user = UserModel.objects.filter(nickname=nickname,is_delete=False).first()
            if user:
                return Response({"code": 204,"message": "昵称已存在"})

            user = UserModel.objects.filter(mobile=mobile,is_delete=False).first()
            if user:
                return Response({"code": 204,"message": "手机号已注册"})
            hash_pwd = make_password(password)
            UserModel.objects.create(nickname=nickname, password=hash_pwd, mobile=mobile, role_id=1)# 默认普通用户
            return Response({"code":200,"message":"注册成功"})

        except Exception as e:
            print(">>>>>>>>>>>>>>>>>>>>报错", e)
            return Response({"code": 500, "message": "后端服务器错误"})

class UserViewsLogin(APIView):
    def post(self, request):
        try:
            mobile = request.data.get("mobile")
            password = request.data.get("password")

            user = UserModel.objects.filter(mobile=mobile,is_delete=False).first()
            if not user:
                return Response({"code": 204,"message": "用户不存在"})

            if check_password(password, user.password):
                user.last_login = datetime.datetime.now()
                user.save()

                payloads = {
                    "user_id": user.id,
                    "nickname": user.nickname,
                }

                token, refresh_token = login_token(payloads)
                ser = UserSer(user)
                return Response({"code": 200, "message": "登录成功", "token": token, "refresh_token": refresh_token, "user_dict": ser.data,})
            return Response({"code": 204, "message": "用户名或密码错误"})
        except Exception as e:
            print("错误",e)
            return Response({"code": 500, "message": "后端服务器错误"})


# 用户信息
class UserViews(ModelViewSet):
    queryset = UserModel.objects.all()
    serializer_class = UserSer


# 收货地址
class AddressViews(ViewSet):

    def post_add(self, request):
        try:
            id = request.data.get("id")
            address = request.data.get("address")
            user = AddressModel.objects.create(user_id=id, address=address)
            return Response({"code": 200,"message": "添加成功"})
        except Exception as e:
            print("地址》》》》》》》》》》》》》》》》》", e)
            return Response({"code": 500, "message": "后端服务器错误"})

    def get_add(self, request):
        try:
            id = request.data.get("id")
            add = AddressModel.objects.get(user=id)
            add_ser = AddressSer(add)
            return Response({"code": 200,"message": "查询成功", "add":add_ser.data})

        except Exception as e:
            print("地址》》》》》》》》》》》》》》》》》》》", e)
            return Response({"code": 500,"message": "服务器错误"})

    def put_add(self, request):
        try:
            id = request.data.get("id")
            address = request.data.get("address")
            AddressModel.objects.filter(id=id).update(address=address)
            return Response({"code": 200,"message": "修改成功"})

        except Exception as e:
            print("地址》》》》》》》》》》》》》》》》》》》",e)
            return Response({"code": 500,"message": "服务器错误"})

    def del_add(self,request,id):
        try:
            AddressModel.objects.get(id=id).delete()
            return Response({"code": 200,"msg": "删除成功"})
        except Exception as e:
            return Response({"code": 204,"msg": "删除失败"})


































